<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","nobkt");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_closing_openperiod.php";
$FileTemplate  = "ais_closing_openperiod_list.html";
$TableName     = "ais_advance";
$SubHeaderTitle= "Closing - Open Period";
$PageSize      = 10;
$HTMLGridList  = "ClosingPeriod";

//************ FORM ACTION **************
if (GetParam("Action","")=="Open Period") {

    //$rai = GetParam("periodMonth","");
    //echo "$rai"; exit;
    //$Test2 = RetPeriod(GetParam("periodMonth",""),GetParam("periodYear",""));
    //echo "$Test2"; exit;
    //$Test = CheckData(GetParam("periodMonth",""),GetParam("periodYear",""));
    //echo "$Test"; exit;

    OpenPeriod (GetParam("periodMonth",""),GetParam("periodYear",""));
        $AlertStr = "<script>alert('Process is Completed!')</script>";
        print $AlertStr;
}

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("monthformblk",GetArrayMonth());
$TBS->MergeBlock("yearformblk",GetArrayYear());
$PageNum = 1;
if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");
$SQLCount = "SELECT count(*) FROM ais_history_ledger ORDER BY ledger DESC";

$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;
$SQL = "SELECT * FROM ais_history_ledger ORDER BY ledger DESC";
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize);

$TBS->Show();

function RetPeriod ($month,$year) {
    $y = substr($year,-2);
    if ($month<10) $month = "0".intval($month);
    return $y.$month;
}

function OpenPeriod ($month,$year) {
    global $DBConnection;

    //Rama, (03 September 2009), Start
    $SQL = "SELECT COUNT(*) FROM ais_history_ledger WHERE year='".$year."' AND month='".$month."'";
    $ResultCurrentPeriod = $DBConnection->dbc->get_var($SQL);
    //echo "$ResultCurrentPeriod"; exit;

    $CurrentPeriod = RetPeriod($month,$year);
    $SQL = "SELECT COUNT(*) as jumlah FROM ais_history_ledger WHERE ledger > '$CurrentPeriod'";
    //$ResultNextLdg = mysql_query($SQL);
    $BufferNextPeriod = $DBConnection->dbc->get_var($SQL); //mysql_fetch_row($ResultNextLdg);
    //$BufferNextPeriod = $GetValue[0];
    //$test1 = GetParam("periodMonth","");
    //$test2 = GetParam("periodYear","");
    //echo "$ResultCurrentPeriod, $SQL, $BufferNextPeriod, $CurrentPeriod, $test1, $test2"; exit;

    $LastPeriod = RetPeriod($month-1,$year);
    $SQL = "SELECT status FROM ais_history_ledger WHERE ledger > '$LastPeriod'";
    //$ResultLastLdg = mysql_query($SQL);
    $BufferLastPeriod = $DBConnection->dbc->get_var($SQL); //mysql_fetch_row($ResultLastLdg);
    //$BufferLastPeriod = $GetValue[0];

    $ForAuditPeriod = RetPeriod($month-1,$year-1);
    $SQL = "SELECT status FROM ais_history_ledger WHERE ledger > '$ForAuditPeriod'";
    $ResultForAudit = mysql_query($SQL);
    $GetValue = mysql_fetch_row($ResultForAudit);
    $BufferForAudit = $GetValue[0];


    if($ResultCurrentPeriod = 1){
        if(($BufferNextPeriod > 0) or ($BufferNextPeriod <= 0 and ($BufferLastPeriod == "Close" or $BufferForAudit == "Close")))
            $SQL = "UPDATE ais_history_ledger SET status='Open' WHERE year='".$year."' AND month='".$month."'";
            $DBConnection->dbc->query($SQL);
    } else
    if($ResultCurrentPeriod == 0){
        if(($BufferNextPeriod > 0) or ($BufferNextPeriod <= 0 and ($BufferLastPeriod == "Close" or $BufferForAudit == "Close")))
            $SQL = "INSERT INTO ais_history_ledger (ktrasl,year,month,ledger,status) VALUES ('".GetOfficeID()."','$year','$month','".RetPeriod ($month,$year)."','Open')";
            $DBConnection->dbc->query($SQL);
    }
    //Rama, (03 September 2009), End

    //$SQL = "UPDATE ais_history_ledger SET status='Open' WHERE year='".$year."' AND month='".$month."'";
    //$DBConnection->dbc->query($SQL);

}

function ClosingPeriod($month,$year) {
    global $DBConnection;
    if ($month<10) $month = "0".intval($month);
    $DBConnection->dbc->query("UPDATE ais_history_ledger SET status='Close' WHERE ledger='".RetPeriod ($month,$year)."' AND year='$year' AND month='month'");
    $month++;
    if ($month<10) $month = "0".intval($month);
    if ($month>12) {$month = "01";$year  = $year+1;}
    $DBConnection->dbc->query("INSERT INTO ais_history_ledger (ktrasl,year,month,ledger,status) VALUES ('".GetOfficeID()."','$year','$month','".RetPeriod ($month,$year)."','Open')");
}
?>
